// script.js
class AssignmentManager {
    constructor() {

        //this.assignments = JSON.parse(localStorage.getItem('assignments')) || [];
        var that =[];
        $.ajax({
            url: '/work/query',
            type: 'get',
            dataType: 'json',
            async: false,
            success: function(data) {
                console.log(data)
                that =data;
            }
        })
        this.assignments =that;
        this.nextId = this.getNextId();
        this.init();
    }

    init() {
        this.renderAssignments();
        this.bindEvents();
    }

    getNextId() {
        return this.assignments.length > 0 
            ? Math.max(...this.assignments.map(a => a.id)) + 1 
            : 1;
    }

    bindEvents() {
        const form = document.getElementById('assignmentForm');
        form.addEventListener('submit', (e) => {
            // 阻止表单提交
            e.preventDefault();
            this.addAssignment();
        });
    }

    addAssignment() {
        const name = document.getElementById('assignmentName').value;
        const description = document.getElementById('assignmentDescription').value;
      //  const url = document.getElementById('assignmentUrl').value;
        //alert(description)
        if (!name || !description ) {
            alert('请填写所有字段');
            return;
        }
      //  alert(1)
        var that = this
        $.ajax({
            url: '/work/add',
            type: 'POST',
            data: {
                title: name,
                descript: description,
            },
            success: function(data) {
                console.log(data);
                //刷新
                window.location.reload();
                // 重新渲染
                that.renderAssignments();
                // 重置表单
                that.resetForm();

            }
        })
        //alert(2)
        // const assignment = {
        //     id: this.nextId++,
        //     name: name,
        //     description: description,
        //     url: url,
        //     createdAt: new Date().toLocaleDateString('zh-CN')
        // };

       // this.assignments.push(assignment);
        //this.saveToLocalStorage();

    }

    // saveToLocalStorage() {
    //     localStorage.setItem('assignments', JSON.stringify(this.assignments));
    // }

    resetForm() {
        document.getElementById('assignmentForm').reset();
    }

    renderAssignments() {
        const grid = document.getElementById('assignmentsGrid');
        grid.innerHTML = '';

        if (this.assignments.length === 0) {
            grid.innerHTML = '<p>暂无作业，请添加第一个作业</p>';
            return;
        }

        this.assignments.forEach((assignment, index) => {
            const card = this.createAssignmentCard(assignment, index + 1);
            grid.appendChild(card);
        });
    }

    createAssignmentCard(assignment, number) {
        const card = document.createElement('div');
        card.className = 'assignment-card';
        card.addEventListener('click', () => {
            window.open("/view/stuList/"+assignment.id, '_blank');
        });

        card.innerHTML = `
            <div class="card-header">
                <span class="assignment-number">作业${number}</span>
                ${assignment.title}
            </div>
            <div class="card-body">
                <p class="description">${assignment.descript}</p>
            </div>
            <div class="card-footer">
                创建时间: ${assignment.systime}
            </div>
        `;

        return card;
    }
}

// 初始化应用
document.addEventListener('DOMContentLoaded', () => {
    new AssignmentManager();
});
